//
//  ShangPinLeiMuCustomView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/21.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "ShangPinLeiMuCustomView.h"
#import "SPLMCell.h"
#import "SPLMModel.h"

@interface ShangPinLeiMuCustomView() <UITableViewDelegate,
UITableViewDataSource>
/**tv*/
@property (nonatomic, strong) UITableView *tv;
/**数据源*/
@property (nonatomic, strong) NSMutableArray *dataArrM;
@end

@implementation ShangPinLeiMuCustomView

#pragma mark - 一 init

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.tv];
    self.tv.tableFooterView = [UIView new];
}

//因为 self 在外面的懒加载中每给frame 。所以在上面的方法中设置tv的frame 等于是 CGRectZero
- (void)layoutSubviews {
    [super layoutSubviews];
    self.tv.frame = self.bounds;
}
#pragma mark - 二  代理方法 UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArrM.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SPLMCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SPLMCell"];
    
    SPLMModel *model = self.dataArrM[indexPath.row];
    cell.model = model;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //图片比例2:1 ; 等比缩放
    CGFloat cellW = SCREEN_WIDTH;
    CGFloat cellH = cellW *0.5;
    return FDHeightScale(cellH);
//    return FDHeightScale(130);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //点击跳转到外送点擦页面。
    
    SPLMModel *currentModel = self.dataArrM[indexPath.row];
    
    if (self.splmCustomViewClickCellblock) {
        self.splmCustomViewClickCellblock(currentModel);
    }
}


#pragma mark - 三 请求数据
- (void)requestShangPinLeiMiData {
    [MBProgressHUD fd_showHUDToView:self]; //菊花
    
    [FDHttpAPi requestShangPinNeiMuSuccessBlock:^(id responseObjc) {
        [MBProgressHUD fd_hideHUDFromView:self];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
//            FDLog(@"请求到数据:%@",responseObjc);
            
            NSArray *arr = responseObjc[@"data"];
            for (NSDictionary *dic in arr) {
                SPLMModel *model = [SPLMModel mj_objectWithKeyValues:dic];
                [self.dataArrM addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tv reloadData];
            });
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
        [MBProgressHUD fd_hideHUDFromView:self]; //隐藏菊花
    }];
}


#pragma mark  - 四
- (UITableView *)tv {
    if (!_tv) {
        //如果是group模式的上下会多出来一部分:上下各多出来一部分。
        //如果是plain模式就没有问题。 正常,但是如果要是有分区的话,会有粘连效果。
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        //代理
        _tv.delegate = self;
        _tv.dataSource = self;
        //注册cell
        [_tv registerNib:[UINib nibWithNibName:@"SPLMCell" bundle:nil] forCellReuseIdentifier:@"SPLMCell"];
        _tv.backgroundColor = [UIColor whiteColor];
        //取消cell线
        _tv.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tv.showsVerticalScrollIndicator = NO;
    }
    return _tv;
}

- (NSMutableArray *)dataArrM {
    if (!_dataArrM) {
        _dataArrM = [NSMutableArray array];
    }
    return _dataArrM;
}

@end
